<?php
namespace app\rbac\controller;
use app\base\controller\Base;
use app\rbac\model\PrivilegeM;
use think\Db;
use think\Session;

class Privilege extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 新增
     * @return mixed
     */
    public function add()
    {
        $model = new PrivilegeM();
        if($this->request->isPost())
        {
            $data = $this->request->post();
            $model->add($data);
            return json(['code'=>$model->code,'msg'=>$model->msg]);
        }
        $data = $model->getCatTree();
        $this->assign(array(
            '_page_title'=>'添加权限',
            '_page_title_english'=>'Privilege add',
            '_btn_title'=>'权限列表',
            '_btn_link'=>'lst',
            '_page_ico'=>'iconfa-legal',
            'data'=>$data,
        ));
        return $this->fetch();
    }



    /**
     * 编辑页面
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $model = new PrivilegeM();
        if($this->request->isPost())
        {
            $data = $this->request->post();
            $model->edit($data);
            return json(['code'=>$model->code,'msg'=>$model->msg]);
        }
        $PriData = $model->getCatTree();
        $data = Db::table('privilege')->where(['id'=>$id,'is_usable'=>1])->find();
        $this->assign(array(
            '_page_title'=>'编辑权限',
            '_page_title_english'=>'Privilege edit',
            '_btn_title'=>'权限列表',
            '_btn_link'=>url('lst'),
            '_page_ico'=>'iconfa-legal',
            'data'=>$data,
            'PriData'=>$PriData,
        ));
        return $this->fetch();
    }


    /**
     * 获取全部数据
     * @return mixed
     */
    public function lst()
    {
        $model = new PrivilegeM();
        $data = $model->getCatTree();
        $this->assign(array(
            '_page_title'=>'权限列表',
            '_page_title_english'=>'Privilege list',
            '_btn_title'=>'添加权限',
            '_btn_link'=>url('add'),
            '_page_ico'=>'iconfa-legal',
            'data'=>$data,
        ));
        return $this->fetch();
    }


    /**
     * 删除数据
     * @param $id
     */
    public function del()
    {
        $id = $this->request->post('id');
        $model = new PrivilegeM();
        $model->del($id);
        return json(['code'=>$model->code,'msg'=>$model->msg]);
    }
}
